package dream.web;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import dream.pojo.UserBlog;
import dream.service.ArticleService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/getuserbyaid")
public class GetUserServlet extends HttpServlet {
    private ArticleService articleService = new ArticleService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String aid = req.getParameter("aid");
        if(!StrUtil.isBlank(aid)) {
            String username = articleService.SelectUser(Integer.valueOf(aid));

            int count = articleService.SelectArticleCount(Integer.valueOf(aid));

            int goodCount = articleService.SelectGoodCount(Integer.valueOf(aid));

            UserBlog userBlog = new UserBlog();

            userBlog.setUsername(username);
            userBlog.setCount(count);
            userBlog.setGoodCount(goodCount);

            // 转为JSON
            String jsonString = JSON.toJSONString(userBlog);

            resp.setContentType("text/json;charset=utf-8");
            resp.getWriter().write(jsonString);

        }else {
            return;
        }

    }
}

